/**********************************************************************
Copyright (c) 2014-2016 Mark C. Erickson

Permission is hereby granted, free of charge, to any person obtaining 
a copy of this software and associated documentation files (the 
"Software"), to deal in the Software without restriction, including 
without limitation the rights to use, copy, modify, merge, publish, 
distribute, sublicense, and/or sell copies of the Software, and to 
permit persons to whom the Software is furnished to do so, subject to 
the following conditions:

The above copyright notice and this permission notice shall be included 
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 

Reference: Rec. ITU-R BS.1770

**********************************************************************/

desc:LUFS Meter, Mono [-18dB], v1.0.1

@init
a1a =     -1.69065929318241;  //  48k prefilter, boost highs, model effects of head as a sphere
a2a =     0.73248077421585;
b0a =     1.53512485958697;
b1a =     -2.69169618940638;
b2a =     1.19839281085285;

srate == 44100 ? (
	a1a =     -1.66365511325602;  //  44k prefilter, boost highs, model effects of head as a sphere
	a2a =     0.71259542807323;
	b0a =     1.53084123005035;
	b1a =     -2.65097999515473;
	b2a =     1.16907907992159;
);

axn=0;
axn1=0;
axn2=0;
ayn=0;
ayn1=0;
ayn2=0;

a1b =     -1.99004745483398;  //  48k High Pass
a2b =     0.99007225036621;
b0b =     1.0;
b1b =     -2.0;
b2b =     1.0;

srate == 44100 ? (
	a1b =     -1.98916967362980;  //  44k High Pass
	a2b =     0.98919903578704;
	b0b =     0.99956006454251;
	b1b =     -1.99912012908503;
	b2b =     0.99956006454251;
);

bxn=0;
bxn1=0;
bxn2=0;
byn=0;
byn1=0;
byn2=0;

buf = 0;                  //  buffer exists at offset 0
bufpos = 0;
buflength = srate * 3;//1.2;  //  1200 ms

rmsSum = 0;
rmsTarget = 0.01;
LUFS_REF = pow(10, 20/-18);


@sample
axn2=axn1;
axn1=axn;
axn=spl0;
ayn2=ayn1;
ayn1=ayn;

ayn=b0a*axn + b1a*axn1 + b2a*axn2 - a1a*ayn1 - a2a*ayn2;
 
bxn2=bxn1;
bxn1=bxn;
bxn=ayn * sqrt(1.3);
byn2=byn1;
byn1=byn;

byn=b0b*bxn + b1b*bxn1 + b2b*bxn2 - a1b*byn1 - a2b*byn2;

rmsValue = byn * byn;
rmsSum -= buf[bufpos];
buf[bufpos] = rmsValue;
rmsSum += rmsValue;

bufpos = bufpos + 1 ;
bufpos > buflength ? bufpos = 0;

gain = 1.0;
rmsDelta = rmsSum / buflength;
rmsDB = 20 * log10(sqrt(rmsDelta) / LUFS_REF) - 24; // -21 for stereo simulation

spl0 = axn;
spl1 = spl1;


@gfx 100 18
gfx_x=gfx_y=5;
gfx_r=0.7;
gfx_g=1;
gfx_b=0.1;
gfx_a=1;
gfx_drawchar($'L');
gfx_drawchar($'U');
gfx_drawchar($'F');
gfx_drawchar($'S');
gfx_drawchar($' ');
gfx_drawchar($'1');
gfx_drawchar($'8');
gfx_drawchar($':');
gfx_drawchar($' ');
gfx_drawnumber(rmsDB,1);

